
****************************************************************************************************
*
* Replication file for: 
*
* Evelyne Hübscher, Thomas Sattler, Markus Wagner. Forthcoming. "Does Austerity Cause Polarization?"
* British Journal of Political Science
*
* Macro Analysis, file 1/2
*
****************************************************************************************************
*
* Date: November 7, 2022
* 
* Software: Stata 17 
*
*
*********************************
* Prepare elections-based dataset
*********************************

use Data/HSW_BJPS_Macro.dta, clear


* NMS vote
* --------
*
* - Step 1: identify mainstream parties using party families in CMP; MS = SOC, LIB, CHR, CON and AGR

foreach num in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 {

	g mainstream_`num' = 1 if parfam_`num' == 30 | parfam_`num' == 40 ///
		| parfam_`num' == 50 | parfam_`num' == 60 | parfam_`num' == 80
	replace mainstream_`num' = 0 if mainstream_`num' != 1
	
}

* - Step 2: identify non-mainstream party vote shares
*
* a) parties listed in CMP dataset, but not identified as mainstream 

foreach num in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 {

	g pervote_`num'_nms = .
	replace pervote_`num'_nms = pervote_`num' if mainstream_`num' == 0

}	

* b) parties not listed in CMP 

egen totalvote = rowtotal(pervote_01 - pervote_16)
g pervote_other = 100-totalvote

* - Step 3: sum up non-mainstream vote shares

egen nms = rowtotal(pervote_01_nms - pervote_16_nms pervote_other)
drop mainstream_01 - mainstream_16 ///
	pervote_01_nms - pervote_16_nms ///
	totalvote

* Polarization
* ------------
*
* - compute standard deviation (sd) of party positions in a country
* - sd is computed based on weighted parties (parties weighted by their vote shares): wpsd
* - Source of party positions: Parlgov left-right
*
* Procedure:
* 
* - Step 1: rescale weights (party vote shares) to vary between 0 and 1
* - Step 2: compute sd of party positions (= square root of sum of deviations from mean (weighted))
*			a) compute weight for each party (vote share / vote share of all recorded parties)
*			b) apply weight to party positions and compute weighted mean of party positions in a country
*			c) compute squared deviation from mean
* 			d) compute sd
* - Step 3: erase auxiliary variables
* 
* - Step 1

foreach num in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 {
	g pervote_`num'_resc = pervote_`num'/100
}

* - Step 2 (NOTE: order of generated variables in this step matters later when we use rowsums!)
*
* a) 

egen sumweights = rowtotal(pervote_01_resc-pervote_16_resc)

foreach num in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 {
	g weight_`num' = pervote_`num'_resc/sumweights
}

* b)

foreach num in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 {
	g weightedpg_`num' = weight_`num'*parlgov_rl_`num'
}

egen pg_wmean = rowtotal(weightedpg_01-weightedpg_16)

* c)

foreach num in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 {
	g pg_`num'_wdevmeansq = weight_`num'*(parlgov_rl_`num' - pg_wmean)^2
}

* d) 

egen sum_weighted_pg = rowtotal(pg_01_wdevmeansq-pg_16_wdevmeansq)
g wpsd = sqrt(sum_weighted_pg)
sum wpsd
replace wpsd = . if wpsd == 0

* - Step 3

drop pervote_01_resc-pervote_16_resc ///
	sumweights ///
	weight_01-weight_16 ///
	weightedpg_01-weightedpg_16 ///
	pg_wmean ///
	pg_01_wdevmeansq-pg_16_wdevmeansq ///
	sum_weighted_pg


* Left, nationalist, green, other NMS parties (for Appendix)
* ----------------------------------------------------------
*
* - Left parties; parfam == 20

foreach num in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 {

	g LEF_`num' = 1 if parfam_`num' == 20 
	replace LEF_`num' = 0 if LEF_`num' != 1
	
}

foreach num in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 {

	g pervote_`num'_LEF = .
	replace pervote_`num'_LEF = pervote_`num' if LEF_`num' == 1

}	

egen left = rowtotal(pervote_01_LEF - pervote_16_LEF)
drop LEF_01 - LEF_16 pervote_01_LEF - pervote_16_LEF 

* - Nationalist parties; parfam == 70

foreach num in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 {

	g NAT_`num' = 1 if parfam_`num' == 70 
	replace NAT_`num' = 0 if NAT_`num' != 1
	
}

foreach num in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 {

	g pervote_`num'_NAT = .
	replace pervote_`num'_NAT = pervote_`num' if NAT_`num' == 1

}	

egen nat = rowtotal(pervote_01_NAT - pervote_16_NAT)
drop NAT_01 - NAT_16 pervote_01_NAT - pervote_16_NAT

* - Ecological parties; parfam == 10

foreach num in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 {

	g ECO_`num' = 1 if parfam_`num' == 10 
	replace ECO_`num' = 0 if ECO_`num' != 1
	
}

foreach num in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 {

	g pervote_`num'_ECO = .
	replace pervote_`num'_ECO = pervote_`num' if ECO_`num' == 1

}	

egen eco = rowtotal(pervote_01_ECO - pervote_16_ECO)
drop ECO_01 - ECO_16 pervote_01_ECO - pervote_16_ECO

* - Other NMS parties

foreach num in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 {

	g othernms_`num' = 1 if parfam_`num' == 90 | parfam_`num' == 95 | parfam_`num' == 98 
	replace othernms_`num' = 0 if othernms_`num' != 1
	
}

foreach num in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 {

	g pervote_`num'_othernms = .
	replace pervote_`num'_othernms = pervote_`num' if othernms_`num' == 1

}	

egen other = rowtotal(pervote_01_othernms - pervote_16_othernms pervote_other)
drop othernms_01 - othernms_16 pervote_01_othernms - pervote_16_othernms pervote_other

	
* Extreme parties - based on Parlgov
* ----------------------------------
* 
* Cutoffs: radical right - parlgov rl >= 7; radical left - parlgov rl <= 3
*
* - Radical left parties

foreach num in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 {

	g EXLEFT_`num' = 1 if parlgov_rl_`num' <= 3 & parlgov_rl_`num' != .
	
}

foreach num in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 {

	g pervote_`num'_EXLEFT = .
	replace pervote_`num'_EXLEFT = pervote_`num' if EXLEFT_`num' == 1

}	

egen radleft = rowtotal(pervote_01_EXLEFT - pervote_16_EXLEFT)
drop EXLEFT_01 - EXLEFT_16 pervote_01_EXLEFT - pervote_16_EXLEFT 

* - Radical right parties

foreach num in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 {

	g EXRIGHT_`num' = 1 if parlgov_rl_`num' >= 7 & parlgov_rl_`num' != .
	
}

foreach num in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 {

	g pervote_`num'_EXRIGHT = .
	replace pervote_`num'_EXRIGHT = pervote_`num' if EXRIGHT_`num' == 1

}	

egen radright = rowtotal(pervote_01_EXRIGHT - pervote_16_EXRIGHT)
drop EXRIGHT_01 - EXRIGHT_16 pervote_01_EXRIGHT - pervote_16_EXRIGHT 

		
* Lags and Differences
* --------------------	

sort country year
by country: g elno = _n
tsset country elno

g lwpsd = l.wpsd
g dwpsd = d.wpsd
g labs = l.abs
g dabs = d.abs
g lnms = l.nms 
g dnms = d.nms
g lleft = l.left
g dleft = d.left
g lnat = l.nat
g dnat = d.nat
g leco = l.eco
g deco = d.eco
g lother = l.other
g dother = d.other
g lradleft = l.radleft
g dradleft = d.radleft
g lradright = l.radright
g dradright = d.radright

	
* Clean up and save
* -----------------

preserve

keep if year >= 1980 // austerity data starts in 1978
keep if year <= 2016 // austerity data ends in 2014
drop if countryname == "Belgium" & year == 2014 // data incomplete - flemish part only for 2014 election
	
keep countryname country year month day elday elmonth country_year period ///
	nms dnms lnms abs dabs labs wpsd dwpsd lwpsd dleft dnat deco dother dradleft dradright ///
	lconsol lconsol_cuts lconsol_tax ///
	lfp_total_1 lfp_total_2 lfp_total_3 lfp_total_4 lfp_total_5 lfp1_date lfp2_date lfp3_date lfp4_date lfp5_date ///
	ldgrowthm lgrowthm ldunempm lunempm ldkofm lkofm ldmig lmig lmpgov early3m elecsystem logdismag 
	
order countryname country year month day elday elmonth country_year period ///
	nms dnms lnms abs dabs labs wpsd dwpsd lwpsd dleft dnat deco dother dradleft dradright ///
	lconsol lconsol_cuts lconsol_tax ///
	lfp_total_1 lfp_total_2 lfp_total_3 lfp_total_4 lfp_total_5 lfp1_date lfp2_date lfp3_date lfp4_date lfp5_date ///
	ldgrowthm lgrowthm ldunempm lunempm ldkofm lkofm ldmig lmig lmpgov early3m elecsystem logdismag 

save Data/HSW_BJPS_Macro_Elections.dta, replace
outsheet using Data/HSW_BJPS_Macro_Elections.csv, replace

restore

************************
* Prepare annual dataset
************************

keep countryname country year month day elday wpsd abs nms 

list countryname year elday if countryname == countryname[_n-1] & year == year[_n-1]
drop if countryname == "Ireland" & year == 1982 & month == 2 // two elections in same year
drop if countryname == "Belgium" & year == 2014 // data incomplete - flemish part only for 2014 election

tsset country year
tsfill 

g original = (countryname != "")
replace countryname = countryname[_n-1] if original == 0
replace elday = l.elday if original == 0
replace wpsd = l.wpsd if original == 0
replace abs = l.abs if original == 0
replace nms = l.nms if original == 0

drop month day

sort countryname year 	
	
expand 3 if countryname == "Japan" & year == 2014, gen(duplicate)
sort countryname year duplicate
replace year = year[_n-1]+1 if duplicate == 1
replace original = 0 if duplicate == 1
drop duplicate 

expand 2 if countryname == "Canada" & year == 2015, gen(duplicate)
sort countryname year duplicate
replace year = year[_n-1]+1 if duplicate == 1
replace original = 0 if duplicate == 1
drop duplicate 

expand 2 if countryname == "Denmark" & year == 2015, gen(duplicate)
sort countryname year duplicate
replace year = year[_n-1]+1 if duplicate == 1
replace original = 0 if duplicate == 1
drop duplicate 

expand 2 if countryname == "Portugal" & year == 2015, gen(duplicate)
sort countryname year duplicate
replace year = year[_n-1]+1 if duplicate == 1
replace original = 0 if duplicate == 1
drop duplicate 

keep if year >= 1978
keep if year <= 2016

save Data/HSW_BJPS_Macro_Annual.dta, replace
outsheet using Data/HSW_BJPS_Macro_Annual.csv, replace


